package com.ding.pda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.pda.constant.PdaConstant;
import com.ding.pda.dto.AddDto;
import com.ding.pda.dto.UpdateDto;
import com.ding.pda.entity.Pda;
import com.ding.pda.mapper.PdaMapper;
import com.ding.pda.service.IPdaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.common.api.dto.PageBean;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * pda 服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-07-21
 */
@Service
public class PdaServiceImpl extends ServiceImpl<PdaMapper, Pda> implements IPdaService {

    @Autowired
    private PdaMapper pdaMapper;

    @Override
    public ResultVo addPda(AddDto addDto) {

        Pda pda = new Pda();
        pda.setTime(new Date());
        pda.setStatus(PdaConstant.PDA_OUTLINE);
        BeanUtils.copyProperties(addDto,pda);

        int insert = pdaMapper.insert(pda);

        if (insert > 0){
            return new ResultVo(ResultCode.SUCCESS,"手持设备添加成功");
        }else{
            return new ResultVo(ResultCode.FAILED,"手持设备添加失败");
        }
    }


    @Override
    public ResultVo updatePda(UpdateDto updateDto) {

        Long id = updateDto.getId();
        Pda pda = pdaMapper.selectById(id);
        Long patrollerId = pda.getPatrollerId();

        if(patrollerId == null){
            pda.setTime(new Date());
            pda.setStatus(PdaConstant.PDA_OUTLINE);
            BeanUtils.copyProperties(updateDto,pda);

            int update = pdaMapper.updateById(pda);

            if (update > 0){
                return new ResultVo(ResultCode.SUCCESS,"手持设备修改成功");
            }else{
                return new ResultVo(ResultCode.FAILED,"手持设备修改失败");
            }

        }else{
            return new ResultVo(ResultCode.FAILED,"巡检员正在登陆状态，无法修改");
        }

    }

    @Override
    public ResultVo deletePda(Integer pdaId) {
        Pda pda = pdaMapper.selectById(pdaId);
        Long patrollerId = pda.getPatrollerId();

        if(patrollerId == null){
            int delete = pdaMapper.deleteById(pdaId);

            if (delete > 0){
                return new ResultVo(ResultCode.SUCCESS,"手持pda设备删除成功");
            }else{
                return new ResultVo(ResultCode.FAILED,"手持pda设备删除失败");
            }
        }else{
            return new ResultVo(ResultCode.FAILED,"巡检员正在登陆状态，无法删除");
        }
    }

    @Override
    public ResultVo findPdaByPages(Integer current, Integer size) {


        LambdaQueryWrapper<Pda> query = Wrappers.lambdaQuery(Pda.class);
        Page<Pda> page = new Page<>(current, size);
        Page<Pda> pdaPage = pdaMapper.selectPage(page, query);

        Long total = pdaPage.getTotal();
        List<Pda> records = pdaPage.getRecords();

        if (!CollectionUtils.isEmpty(records)) {
            PageBean pageBean = new PageBean();
            pageBean.setTotals(total.intValue());
            pageBean.setList(records);

            return new ResultVo(pageBean);
        }

        return new ResultVo(ResultCode.FAILED);

    }

    @Override
    public ResultVo bindDevice(Long pdaId, Long patrollerId, String status) {
        Pda pda = pdaMapper.selectById(pdaId);
        pda.setPatrollerId(patrollerId);
        pda.setStatus(status);

        int update = pdaMapper.updateById(pda);

        if(update>0){
            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);
    }

    @Override
    public ResultVo unbindPda() {

        LambdaQueryWrapper<Pda> query = Wrappers.lambdaQuery(Pda.class);
        query.isNull(Pda::getPatrollerId);
        List<Pda> pdas = this.list(query);

        return new ResultVo(pdas);
    }

}
